{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import sys\n",
    "from operator import add\n",
    "from pyspark import SparkContext\n",
    "from pyspark.streaming import StreamingContext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = SparkContext.getOrCreate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "ssc = StreamingContext(sc, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:43\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:44\n",
      "-------------------------------------------\n",
      "('sdfg', 1)\n",
      "('1234', 1)\n",
      "('456', 1)\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:45\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:46\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:47\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:48\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:49\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:50\n",
      "-------------------------------------------\n",
      "('zfxdsd', 1)\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:51\n",
      "-------------------------------------------\n",
      "('sdfg', 1)\n",
      "('szdf', 1)\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:52\n",
      "-------------------------------------------\n",
      "('sd', 1)\n",
      "('gsdfg', 1)\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:53\n",
      "-------------------------------------------\n",
      "('df', 1)\n",
      "('fgs', 2)\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:54\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:55\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:56\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:57\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:58\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:53:59\n",
      "-------------------------------------------\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 08:54:00\n",
      "-------------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "host = \"127.0.0.1\"\n",
    "port = 9999\n",
    "while True:\n",
    "    lines = ssc.socketTextStream(host, port)\n",
    "    counts = lines.flatMap(lambda line: line.split(\" \"))\\\n",
    "                  .map(lambda word: (word, 1))\\\n",
    "                  .reduceByKey(lambda a, b: a+b)\n",
    "    counts.pprint()\n",
    "\n",
    "    ssc.start()\n",
    "    ssc.awaitTermination()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 新开一个终端，输入 `nc -lk 9999`, 不停的输入就可以实现1s统计单词的数量了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
